<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];
	
	$dist = array();
	
	$bonus_day = date("d",mktime(0, 0, 0 , date('m'), date('d')-1, date("Y")));
	$today_day = date("d",mktime(0, 0, 0 , date('m'), date('d'), date("Y")));
	$bonus_date = date("Y-m-d",mktime(0, 0, 0 , date('m'), date('d'), date("Y")));
	$bonus_month = date("Ym",mktime(0, 0, 0 , date('m'), date('d'), date("Y")));
	$cdate = date('Y-m-d H:i:s'); $cdate = date("Y-m-d H:i:s",mktime(0, 0, 0 , date('m'), date('d'), date("Y")));
	$total_pair_amount = 0;
	$total_bv_amount = 0;
	$max_payout = 0;
	$pairing_rate = 1;
	
	$is_calc = @mysql_result(mysql_query("select count(*) from $db->member_comm_sum where bonus_date = '".$bonus_date."'"), 0);
	
	if($is_calc<=0){
		$r=mysql_query("select a.*,b.amount from gg_users a left join gg_member_cf_calc b on b.uid=a.id where date_format(a.cdate, '%Y-%m-%d')<='".$bonus_date."'");
		while($user = mysql_fetch_assoc($r))
		{
			$dist[$user['matrix_upline']][$user['matrix_position']]['id']=$user['id'];
			
			$dist[$user['id']]['1']['bv']=0;
			$dist[$user['id']]['2']['bv']=0;
			$dist[$user['id']]['ref']=$user['ref'];
			$dist[$user['id']]['ref_level']=$user['ref_level'];
			$dist[$user['id']]['matrix_upline']=$user['matrix_upline'];
			$dist[$user['id']]['matrix_position']=$user['matrix_position'];
			$dist[$user['id']]['s']=0;
			$dist[$user['id']]['p']=0;
			$dist[$user['id']]['m']=0;
			$dist[$user['id']]['cf']=$user['amount']+0;
			$dist[$user['id']]['ps']=0;
			$dist[$user['id']]['pgs']=0;
			$dist[$user['id']]['rank_a']=$user['rank_a'];
			$dist[$user['id']]['max_pairing']=0;
			$dist[$user['id']]['max_cf']=999999999999;
			$dist[$user['id']]['pair_bv']=0;
			$dist[$user['id']]['pair_flush']=0;
			//$dist[$user['id']]['flash_date']=$user['flash_date'];
			
			if($user['rank_a']=='5.0'){
				$dist[$user['id']]['spercentage']=20;
				$dist[$user['id']]['ppercentage']=10;
				$dist[$user['id']]['leadership']=2;
				$dist[$user['id']]['leadership2']=1;
				$dist[$user['id']]['max_pairing']=999999999999;
			}elseif($user['rank_a']=='10.0'){
				$dist[$user['id']]['spercentage']=20;
				$dist[$user['id']]['ppercentage']=10;
				$dist[$user['id']]['leadership']=2;
				$dist[$user['id']]['leadership2']=1;
				$dist[$user['id']]['max_pairing']=999999999999;
			}elseif($user['rank_a']=='100.0'){
				$dist[$user['id']]['spercentage']=20;
				$dist[$user['id']]['ppercentage']=10;
				$dist[$user['id']]['leadership']=2;
				$dist[$user['id']]['leadership2']=1;
				$dist[$user['id']]['max_pairing']=999999999999;
			}elseif($user['rank_a']=='300.0'){
				$dist[$user['id']]['spercentage']=20;
				$dist[$user['id']]['ppercentage']=10;
				$dist[$user['id']]['leadership']=2;
				$dist[$user['id']]['leadership2']=1;
				$dist[$user['id']]['max_pairing']=999999999999;
			}
			
			if($dist[$user['id']]['max_pairing']>0)
				$dist[$user['id']]['limit']=floor(($dist[$user['id']]['max_pairing']/$dist[$user['id']]['ppercentage'])*100) + 0;
		}	
		
		$month_bonus = @mysql_result(mysql_query("select sum(s+p+m) from $db->member_comm_sum where date_format(bonus_date,'%Y%m%d') between '".$bonus_month."01' and '".$bonus_month.$bonus_day."'"), 0);
		$month_bv = @mysql_result(mysql_query("select sum(total_bv) from $db->purchase where date_format(cdate,'%Y%m%d') between '".$bonus_month."01' and '".$bonus_month.$today_day."'"), 0);
		$max_payout = ($month_bv * 2) - $month_bonus;
	
		$rs_sales=mysql_query("select b.id,b.ref,a.total_bv,a.rank_a from gg_purchase a inner join gg_users b on b.id=a.uid where date_format(a.cdate, '%Y-%m-%d')='".$bonus_date."'");
		while($sales = mysql_fetch_assoc($rs_sales)){
			$matrix_upline=$dist[$sales['id']]['matrix_upline'];
			
			if($sales['rank_a']>0 && $sales['total_bv']>0){
				$dist[$sales['id']]['ps']+=$sales['total_bv'];
				$dist[$sales['id']]['pgs']+=$sales['total_bv'];
				
				while($matrix_upline>0){
					$dist[$matrix_upline]['pgs']+=$sales['total_bv'];
					$matrix_upline=$dist[$matrix_upline]['matrix_upline'];
				}
				
				//sponsor bonus
				$max_payout-=($sales['total_bv']*$dist[$sales['ref']]['spercentage']/100);
				$dist[$sales['ref']]['s']+=$sales['total_bv']*$dist[$sales['ref']]['spercentage']/100;
				$sql = "INSERT INTO gg_member_comm SET uid=".$sales['ref'].",from_uid=".$sales['id'].",type='s',volume_type=".$sales['total_bv'].",amount=".($sales['total_bv']*$dist[$sales['ref']]['spercentage']/100).",percent='".$dist[$sales['ref']]['spercentage']."',bonus_date='".$bonus_date."',cdate='".$cdate."'";
				mysql_query($sql);
				
				$total_bv_amount += $sales['total_bv'];
			}
		}
		
		mysql_query("truncate gg_member_cf_calc");
		foreach ($dist as $id=>$avalue){		
			$sql = "INSERT INTO gg_member_cf SET uid=".$id.",leg=".$avalue['matrix_position'].",bv=".$avalue['pgs'].",amount=".($avalue['cf']+$avalue['pgs']).",cdate='".$bonus_date."'";
			mysql_query($sql);
			$sql = "INSERT INTO gg_member_cf_calc SET uid=".$id.",amount=".($avalue['cf']+$avalue['pgs']);
			mysql_query($sql);
			
			$dist[$dist[$id]['matrix_upline']][$dist[$id]['matrix_position']]['bv']=($avalue['cf']+$avalue['pgs'])+0;
		}
		
		foreach ($dist as $id=>$avalue){
			//pairing
			if($dist[$id]['1']['bv']<=0 && $dist[$id]['2']['bv']<=0) continue;
			
			if($dist[$id]['1']['bv']>=$dist[$id]['2']['bv']){
				$pair_bv = $dist[$id]['2']['bv'];
						
				if($dist[$id]['limit']<$pair_bv){
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$pair_bv = $dist[$id]['limit'];
					$dist[$id]['limit'] = 0;
					$dist[$id]['pair_flush'] = $pair_bv-$dist[$id]['limit'];
				}else{
					$dist[$id]['limit'] = $dist[$id]['limit']-$pair_bv;
				}
						
				if($pair_bv>0){				
					$dist[$id]['pair_bv'] = $pair_bv;
					$pair_amount = ceil($pair_bv * ($dist[$id]['ppercentage']/100));
					//$dist[$id]['p']+=$pair_amount;
					//1
					if($dist[$id]['ps']>0){
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['2']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=1,leg1_id=".$dist[$id]['1']['id'].",leg1_amount='".$dist[$id]['1']['bv']."',leg2=2,leg2_id=".$dist[$id]['2']['id'].",leg2_amount='".$dist[$id]['2']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=1,amount2=".$pair_amount;
						//1.30 because of maximum leadership bonus is up 3 x 5% and down 3 x 5%
						$max_payout -= $pair_amount * 1.30; 
					}else{
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['2']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=1,leg1_id=".$dist[$id]['1']['id'].",leg1_amount='".$dist[$id]['1']['bv']."',leg2=2,leg2_id=".$dist[$id]['2']['id'].",leg2_amount='".$dist[$id]['2']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=0,amount2=0";
						$total_pair_amount += $pair_amount;
					}
					mysql_query($sql);
						
					$sql = "UPDATE gg_member_cf_calc SET amount=0 WHERE uid=".$dist[$id]['2']['id'];
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET pair_amount=".$pair_bv." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$dist[$id]['1']['bv'] = $dist[$id]['1']['bv']-$dist[$id]['2']['bv'];
					$dist[$id]['2']['bv'] = 0;
					$sql = "UPDATE gg_member_cf_calc SET amount=".$dist[$id]['1']['bv']." WHERE uid=".$dist[$id]['1']['id'];
					mysql_query($sql);
				}
			}elseif($dist[$id]['2']['bv']>=$dist[$id]['1']['bv']){
				$pair_bv = $dist[$id]['1']['bv'];
						
				if($dist[$id]['limit']<$pair_bv){
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$pair_bv = $dist[$id]['limit'];
					$dist[$id]['limit'] = 0;
					$dist[$id]['pair_flush'] = $pair_bv-$dist[$id]['limit'];
				}else{
					$dist[$id]['limit'] = $dist[$id]['limit']-$pair_bv;
				}
						
				if($pair_bv>0){
					$dist[$id]['pair_bv'] = $pair_bv;
					$pair_amount = ceil($pair_bv * ($dist[$id]['ppercentage']/100));
					//$dist[$id]['p']+=$pair_amount;
					
					if($dist[$id]['ps']>0){
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['1']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=2,leg1_id=".$dist[$id]['1']['id'].",leg1_amount='".$dist[$id]['2']['bv']."',leg2=2,leg2_id=".$dist[$id]['1']['id'].",leg2_amount='".$dist[$id]['1']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=1,amount2=".$pair_amount;
						//1.30 because of maximum leadership bonus is up 3 x 5% and down 3 x 5%
						$max_payout -= $pair_amount * 1.30; 
					}else{
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['1']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=2,leg1_id=".$dist[$id]['2']['id'].",leg1_amount='".$dist[$id]['2']['bv']."',leg2=1,leg2_id=".$dist[$id]['1']['id'].",leg2_amount='".$dist[$id]['1']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=0,amount2=0";
						$total_pair_amount += $pair_amount;
					}
					mysql_query($sql);
						
					$sql = "UPDATE gg_member_cf_calc SET amount=0 WHERE uid=".$dist[$id]['1']['id'];
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET pair_amount=".$pair_bv." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$dist[$id]['2']['bv'] = $dist[$id]['2']['bv']-$dist[$id]['1']['bv'];
					$dist[$id]['1']['bv'] = 0;
					$sql = "UPDATE gg_member_cf_calc SET amount=".$dist[$id]['2']['bv']." WHERE uid=".$dist[$id]['2']['id'];
					mysql_query($sql);
				}
			}
					
			$flash = 0;
			//$max_bv = $dist[$dist[$id]['matrix_upline']]['max_cf'];
			$max_bv = $dist[$id]['max_cf'];
			if($dist[$id]['1']['bv']>$max_bv){
				$flash = $dist[$id]['1']['bv'] - $max_bv; 
				$sql = "UPDATE gg_member_cf SET flash_amount=flash_amount+".$flash." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
				mysql_query($sql);
				$sql = "UPDATE gg_member_cf_calc SET amount=amount-".$flash." WHERE uid=".$dist[$id]['1']['id'];
				mysql_query($sql);
				$dist[$id]['pair_flush'] += $flash;
				//print_r("1 = ".$dist[$id]['matrix_upline']." = ".$dist[$id]['1']['bv']." = ".$max_bv." = ".$flash."<br>");
			}
			if($dist[$id]['2']['bv']>$max_bv){
				$flash = $dist[$id]['2']['bv'] - $max_bv; 
				$sql = "UPDATE gg_member_cf SET flash_amount=flash_amount+".$flash." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
				mysql_query($sql);
				$sql = "UPDATE gg_member_cf_calc SET amount=amount-".$flash." WHERE uid=".$dist[$id]['2']['id'];
				mysql_query($sql);
				$dist[$id]['pair_flush'] += $flash;
				//print_r("2 = ".$dist[$id]['matrix_upline']." = ".$dist[$id]['1']['bv']." = ".$max_bv." = ".$flash."<br>");
			}		
		}
		
		
		if($total_bv_amount>0 && $total_pair_amount>0){
			
			$pairing_rate = 1;
			mysql_query("UPDATE gg_member_comm SET percent2=".$pairing_rate.",amount2=amount*".$pairing_rate." WHERE type='p' AND bonus_date='".$bonus_date."' AND percent2<=0 AND amount2<=0");
			
			$rs_pairing=mysql_query("SELECT uid,amount2 FROM gg_member_comm WHERE type='p' AND bonus_date='".$bonus_date."'");
			while($pairing = mysql_fetch_assoc($rs_pairing)){
				$dist[$pairing['uid']]['p']+=$pairing['amount2'];
				
				//up 5
				if($dist[$pairing['uid']]['p']>0 && $dist[$pairing['uid']]['ref']>0){
					$level=1;
					$ref = $dist[$pairing['uid']]['ref'];
					$leader_comm = $dist[$pairing['uid']]['p']*0.1;
					while($level<=2){
						if($level<=$dist[$ref]['leadership']){
							$dist[$ref]['m']+=$leader_comm;
										
							$sql = "INSERT INTO gg_member_comm SET uid=".$ref.",from_uid=".$pairing['uid'].",type='m',volume_type=".$dist[$pairing['uid']]['p'].",percent=10.00,level=".$level.",amount=".$leader_comm.",bonus_date='".$bonus_date."',cdate='".$cdate."'";
							mysql_query($sql);	
						}
							
						$level++;					
						$ref = $dist[$ref]['ref'];
						
						if($ref<=0) break;
					}
				}
				
				//down 1
				if($dist[$pairing['uid']]['p']>0){
					$level=1;
					$ref = $dist[$pairing['uid']]['ref'];
					$ref_level = $dist[$pairing['uid']]['ref_level'];
					$leader_comm = $dist[$pairing['uid']]['p']*0.1;
					$pairing_uid = $pairing['uid'];
					while($level<=1){
						if($pairing_uid>0){
							//$ttl_downlines = @mysql_num_rows($rs_downline=mysql_query("SELECT d.id FROM gg_users s INNER JOIN gg_users d ON d.ref_left BETWEEN s.ref_left AND s.ref_right AND date_format(d.cdate, '%Y-%m-%d')<='".$bonus_date."' AND d.ref_level=s.ref_level+".$level." WHERE s.id=".$pairing['uid']));
							if($level==3){
								$ttl_downlines = @mysql_num_rows($rs_downline=mysql_query("SELECT d.id FROM gg_users s INNER JOIN gg_users d ON d.ref_left BETWEEN s.ref_left AND s.ref_right AND date_format(d.cdate, '%Y-%m-%d')<='".$bonus_date."' AND d.ref_level=s.ref_level+".$level." AND d.rank_a>1 WHERE s.id=".$pairing['uid']));
							}else{
								$ttl_downlines = @mysql_num_rows($rs_downline=mysql_query("SELECT d.id FROM gg_users s INNER JOIN gg_users d ON d.ref_left BETWEEN s.ref_left AND s.ref_right AND date_format(d.cdate, '%Y-%m-%d')<='".$bonus_date."' AND d.ref_level=s.ref_level+".$level." WHERE s.id=".$pairing['uid']));
							}
							
							if($ttl_downlines>0){
								$per_member = $leader_comm / $ttl_downlines;
								while($downline = mysql_fetch_assoc($rs_downline)){
									$dist[$downline['id']]['m']+=$per_member;
									
									$sql = "INSERT INTO gg_member_comm SET uid=".$downline['id'].",from_uid=".$pairing['uid'].",type='m',volume_type=".$dist[$pairing['uid']]['p'].",percent=10.00,level=".$level.",level2='1',amount=".$per_member.",bonus_date='".$bonus_date."',cdate='".$cdate."'";
									mysql_query($sql);			
								}	
							}						
						}
						$level++;
					}
				}
			}
		}
			
		foreach ($dist as $distid=>$value) {
			if($distid==1) continue;
			
			if($value['s']<=0) $value['s'] = 0;
			if($value['p']<=0) $value['p'] = 0;
			if($value['m']<=0) $value['m'] = 0;
			$ttl_amount = $value['s']+$value['p']+$value['m'];
			
			$sql = "INSERT INTO gg_member_comm_sum SET uid=".$distid.",pgs=".$value['pgs'].",lcf=".$value['1']['bv'].",rcf=".$value['2']['bv'].",pbv=".$value['pair_bv'].",fbv=".$value['pair_flush'].",s=".$value['s'].",p=".$value['p'].",m=".$value['m'].",bonus_date='".$bonus_date."',cdate='".$cdate."'";
			mysql_query($sql);
			print_r($sql."<br><br>");
			if($ttl_amount>0){
				$ewallet = $ttl_amount*1;
				$swallet = $ttl_amount*0;
				
				//e-wallet
				if($ewallet>0){
					$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$distid), 0) or 0;			
					$balance+=$ewallet;
					mysql_query("update gg_users set ewallet=".$balance." where id=".$distid);			
					$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='credit',amount=".$ewallet.",bal=".$balance.",descr='Daily Bonus [".$ttl_amount." * 90%] ".$bonus_date."',cdate='".$cdate."'";
					mysql_query($sql);
				}
				//sponsor fund
				if($swallet>0){
					$balance=mysql_result(mysql_query("select swallet from gg_users where id=".$distid), 0) or 0;			
					$balance+=$swallet;
					mysql_query("update gg_users set swallet=".$balance." where id=".$distid);			
					$sql = "INSERT INTO gg_member_swallet_record SET uid=".$distid.",type='credit',amount=".$swallet.",bal=".$balance.",descr='Daily Bonus [".$ttl_amount." * 10%] ".$bonus_date."',cdate='".$cdate."'";
					mysql_query($sql);
				}
			}		
		}
		
		mysql_query("update gg_member_comm set status='confirmed' where bonus_date='".$bonus_date."'");
		print_r('finish');
	}
}
?>